﻿using PolarImport;
using PolarImport.Settings;
using System;
using System.Drawing;
using System.Globalization;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Windows.Forms;

using ZoneFiveSoftware.Common.Visuals.Fitness;

namespace PolarImport.Settings
{
    public class PolarDevice
    {
        private PolarSeries _polarSerie;
        private MonitorType _monitorType;
        private string _name;
        private string _modells;
        private Guid _guid;
        private List<DeviceConnection> _uploadConnections;
        private List<DeviceConnection> _downloadConnections;
        private bool _basicValues;
        private bool _timeBasedValues;
        private bool _lapData;

        public PolarDevice(XmlNode xmlDeviceNode)
        {
            _polarSerie = (PolarSeries)Enum.Parse(typeof(PolarSeries), xmlDeviceNode.Attributes["Series"].Value);
            _monitorType = (MonitorType)Enum.Parse(typeof(MonitorType), xmlDeviceNode.Name);
            _modells = xmlDeviceNode.Attributes["Modells"].Value;
            _name = xmlDeviceNode.Name;
            _guid = new Guid(xmlDeviceNode.Attributes["GUID"].Value);
            _uploadConnections = new List<DeviceConnection>();
            string[] connections = xmlDeviceNode.Attributes["UploadConnections"].Value.Split(new char[] { ',' });
            foreach (string connection in connections)
            {
                _uploadConnections.Add((DeviceConnection)Enum.Parse(typeof(DeviceConnection), connection));
            }
            _downloadConnections = new List<DeviceConnection>();
            connections = xmlDeviceNode.Attributes["DownloadConnections"].Value.Split(new char[] { ',' });
            foreach (string connection in connections)
            {
                _downloadConnections.Add((DeviceConnection)Enum.Parse(typeof(DeviceConnection), connection));
            }
            _basicValues = bool.Parse(xmlDeviceNode.Attributes["BasicValues"].Value);
            _timeBasedValues = bool.Parse(xmlDeviceNode.Attributes["TimeBasedValues"].Value);
            _lapData = bool.Parse(xmlDeviceNode.Attributes["LapData"].Value);

            
        }
       
        public override string ToString()
        {
            return _modells.ToString();
        }

        public PolarSeries PolarSerie
        {
            get { return _polarSerie; }
            //set { _polarSerie = value; }
        }
        public MonitorType Monitor
        {
            get { return _monitorType; }
            //set { _monitorType = value; }
        }
        public string Name
        {
            get { return _name; }
            //set { _name = value; }
        }
        public string Modells
        {
            get { return _modells; }
            //set { _modells = value; }
        }
        public Guid GUID
        {
            //set { _guid = value; }
            get { return _guid; }
        }

        public List<DeviceConnection> UploadConnections
        {
            //set { _uploadConnections = value; }
            get { return _uploadConnections; }
        }

        public List<DeviceConnection> DownloadConnections
        {
            //set { _downloadConnections = value; }
            get { return _downloadConnections; }
        }
        public bool BasicValues
        {
            get { return _basicValues; }
        }
        public bool TimeBasedValues
        {
            get { return _timeBasedValues; }
        }
        public bool LapData
        {
            get { return _lapData; }
        }

    }

    internal class Settings
    {
        private static IList<PolarDevice> _supportedDevices;

        static Settings()
        {

            _supportedDevices = new List<PolarDevice>();
            reset();

            //// Now try to load the saved settings...
            //if (!load())
            //{
            //    Directory.CreateDirectory(Environment.GetEnvironmentVariable("APPDATA") + "/PolarImport/");
            //    reset();
            //    save();
            //}
        }

        public static IList<PolarDevice> SupportedDevices
        {
            get { return _supportedDevices; }
        }

        //private static bool load()
        //{
        //    if (!File.Exists(Plugin.PluginPath))
        //    {
        //        return false;
        //    }

        //    XmlDocument document = new XmlDocument();
        //    XmlReader reader = new XmlTextReader(Plugin.PluginPath);
        //    document.Load(reader);
        //    reader.Close();
        //    return true;
        //}

        public static void reset()
        {
            // First, get all supported devices.
            XmlDocument supportedDevices = new XmlDocument();
            string resourceName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".SupportedDevices.xml";            
            System.IO.Stream resourceStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
            XmlTextReader suppDevReader = new XmlTextReader(resourceStream);
            supportedDevices.Load(suppDevReader);
            XmlNode suppDevRoot = supportedDevices["SupportedDevices"];
            // Iterate through the Series...
            foreach (XmlNode supportedSerie in suppDevRoot.ChildNodes)
            {
                string seriesName = supportedSerie.Name;
                // Now iterate through all models of this series
                foreach (XmlNode supportedModel in supportedSerie.ChildNodes)
                {
                    XmlAttribute attribute = supportedModel.Attributes != null ? supportedModel.Attributes["Series"] : null;
                    if (attribute != null && attribute.Value == seriesName)
                    {
                        _supportedDevices.Add(new PolarDevice(supportedModel));
                    }
                }
            }
        }

        //public static void save()
        //{
        //    XmlDocument document = new XmlDocument();
        //    XmlElement newChild = document.CreateElement("PolarImport");
        //    document.AppendChild(newChild);
        //    XmlElement element2 = document.CreateElement("Devices");
        //    newChild.AppendChild(element2);
        //    foreach (PolarDevice device in _supportedDevices)
        //    {
        //        XmlElement element3 = document.CreateElement(device.PolarSerie.ToString());
        //        element2.AppendChild(element3);
        //        element3.SetAttribute("Name", device.Name.ToString());
        //    }
        //    XmlTextWriter w = new XmlTextWriter(Plugin.PluginPath , Encoding.UTF8);
        //    w.Formatting = Formatting.Indented;
        //    w.Indentation = 3;
        //    w.IndentChar = ' ';
        //    document.WriteContentTo(w);
        //    w.Close();
        //}

        //public static IList<PolarDevice> AllDevices
        //{
        //    get
        //    {
        //        return _supportedDevices;
        //    }
        //}

        //public static PolarDevice GetPolarDevice(PolarSeries PolarSerie)
        //{
        //    for (int device = 0; device < _supportedDevices.Count; device++)
        //    {
        //        if (_supportedDevices[device].PolarSerie == PolarSerie)
        //            return _supportedDevices[device];
        //    }
        //    return null;
        //}

    }
}

